$(function(){
    $("#list").mothinTreegrid({
        url:'system/dictionary!queryItems.action?dictionaryType.id='+$("#dictionaryTypeId").val(),
        treeField:'name',
        columns:[[
            {field:'name',title:'名称',width:130,sortable:true,formatter:$.mothin.util.htmlEncode},
            {field:'nameDisplay',title:'显示名',width:130,sortable:true,formatter:function(value,row){
                if(value){
                    return value;
                }
                return row.name;
            }},
            {field:'memo',title:'备注',width:200,sortable:true,formatter:$.mothin.util.htmlEncode},
            {field:'id',title:'操作',width:200,fixed:true,resizable:false,formatter:function(value,row){
                var data=frame_queryOperation(History.getState().url);
                if(data&&data.rows&&data.rows.length>0){
                    var link="";
                    var treeStructure=$('#treeStructure').val()=='true';
                    for(var i=0;i<data.rows.length;i++){
                        if(data.rows[i].action == "system/dictionary!deleteItem.action") {
                            if(row.system) {
                                continue;
                            }
                        }else if(data.rows[i].action == "system/dictionary!addChildItem.action") {
                            if(!treeStructure) {
                                continue;
                            }
                        }
                        link+='<button class="btn btn-link btn-xs" onclick="handle(\''+data.rows[i].id+'\',\''+data.rows[i].action+'\',\''+data.rows[i].name+'\',\''+value+'\');"><span class="'+data.rows[i].ico+'" />&nbsp;'+data.rows[i].name+'</button>';
                    }
                    return link;
                }
            }}
        ]]
    },handle);
});

function handle(id,action,name,bizId) {
    switch(action){
        case 'system/dictionary!addItem.action':
            showDialog(action+"?dictionaryType.id="+$("#dictionaryTypeId").val(),'添加新字典项',800,400,function(){
                $("#list").treegrid("reload");
            });
            break;
        case 'system/dictionary!addChildItem.action':
            showDialog(action+"?dictionaryItem.parentId="+bizId+"&dictionaryType.id="+$("#dictionaryTypeId").val(),'添加新字典项',800,400,function(){
                $("#list").treegrid("reload");
            });
            break;
        case 'system/dictionary!updateItem.action':
            showDialog(action+"?dictionaryItem.id="+bizId+"&dictionaryType.id="+$("#dictionaryTypeId").val(),'修改新字典项',800,400,function(){
                $("#list").treegrid("reload");
            });
            break;
        case 'system/dictionary!deleteItem.action':
            showConfirm('确认删除此字典项吗？',function(){
                $.ajax({
                    type: 'POST',
                    url: action,
                    data: {"dictionaryItem.id":bizId},
                    success: function(data){
                        if(data.ok){
                            showMessage('字典项删除成功',function(){
                                $("#list").treegrid("reload");
                            });
                        }else{
                            showMessage('字典项删除失败：'+data.message,null,'error');
                        }
                    }
                });
            });
            break;
    }
}